package com.dl.mall.handler;

import com.dl.mall.mapper.ProductMapper;
import com.dl.mall.utils.RedisUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @author DL
 * @create 2022-05-14 14:12
 */
@Component
public class ScheduleTask {

    @Resource
    private RedisUtils redisUtils;

    @Resource
    private ProductMapper productMapper;

    // 每分钟同步一次
    @Scheduled(cron = "0 */1 * * * ?")
    //或直接指定时间间隔，例如：5秒
    //@Scheduled(fixedRate=5000)
    private void SyncRedisToMysql() {
        Map<Object, Object> productSpecs = redisUtils.hmget("productSpecs");
        System.out.println("productSpecs = " + productSpecs);
        int i = productMapper.updateAllSpecsStock(productSpecs);
        System.out.println("i = " + i);
    }
}
